package rsj.service.usage.product;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import rsj.api.org.entity.UsageReportParam;
import rsj.sql.product.UsageReportForProductDao;
import rsj.service.export.product.IProductExportCurrentTable;
import rsj.service.usage.IUsageTemplateMethod;

/**
 * 产品使用情况
 *
 * @author yuanqinglong
 * @since 2021/3/3 16:52
 */
@Component
public class UsageReportForProduct implements IUsageTemplateMethod, IProductCalculateUsage {


    @Autowired
    private UsageReportForProductDao usageReportForProductDao;


    @Override
    public Node countStatistic(UsageReportParam usageReportParam) {
        return usageReportForProductDao.countStatistic(usageReportParam);
    }

    @Override
    public TableNode queryTableByPage(UsageReportParam usageReportParam) {
        return usageReportForProductDao.queryTableByPage(usageReportParam);
    }

    /**
     * 生成查询sql
     *
     * @param usageReportParam 参数
     * @return sql
     */
    @Override
    public String generateQueryStatement(UsageReportParam usageReportParam) {
        String queryStatement = IProductExportCurrentTable.exportCurrentTableQueryStatement(usageReportParam);
        queryStatement += " LIMIT #{pageSize} OFFSET #{pageNow}";
        return queryStatement;
    }


    /**
     * 生成统计总数sql
     *
     * @param usageReportParam 参数
     * @return sql
     */
    @Override
    public String generateCountStatement(UsageReportParam usageReportParam) {
        String countSql = "SELECT COUNT(*) FROM (" + IProductExportCurrentTable.exportCurrentTableQueryStatement(usageReportParam) + ") a";
        return countSql;
    }

}
